package com.yjt.admin.system.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yjt.admin.system.entity.ComuseGroup;
import com.yjt.admin.system.form.ComuseGroupForm;
import com.yjt.admin.system.param.ComuseGroupParam;
import com.yjt.admin.system.service.ComuseGroupService;
import com.yjt.admin.system.vo.ComuseGroupVO;
import com.yjt.common.enums.ModulEnum;
import com.yjt.common.enums.OperTypeEnum;
import com.yjt.common.log.annotation.OperLog;
import com.yjt.common.vo.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 常用群组表 前端控制器
 * </p>
 *
 * @author zzp
 * @since 2021-10-12
 */
@RefreshScope // 支持Nacos的动态刷新功能
@RestController
@RequestMapping("/admin/system/comuse-group")
@Api(tags = "常用群组管理")
public class ComuseGroupController {

    @Autowired
    private ComuseGroupService comuseGroupService;

    @PostMapping("/add")
    @ApiOperation(value = "增加常用群组信息", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.COMUSE_GROUP_MODUL, operType = OperTypeEnum.ADD, operDesc = "新增常用群组信息")
    public CommonResult addComUseGroup(@RequestBody ComuseGroupForm comuseGroupForm) {
        comuseGroupService.addComUseGroup(comuseGroupForm);
        return CommonResult.success("常用群组新增成功");
    }


    @DeleteMapping("/delete")
    @ApiOperation(value = "删除常用群组信息", notes = "返回JSONObject", hidden = true)
    @ApiImplicitParam(name = "comGroupId", value = "常用群组id")
    @OperLog(operModul = ModulEnum.COMUSE_GROUP_MODUL, operType = OperTypeEnum.DEL, operDesc = "删除常用群组信息")
    public CommonResult delComUseGroup(String comGroupId) {
        comuseGroupService.removeById(comGroupId);
        return CommonResult.success("常用群组删除成功");
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改常用群组信息", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.COMUSE_GROUP_MODUL, operType = OperTypeEnum.MOD, operDesc = "修改常用群组信息")
    public CommonResult updateComUseGroup(@RequestBody ComuseGroupForm comuseGroupForm) {
        comuseGroupService.updateComUseGroup(comuseGroupForm);
        return CommonResult.success("常用群组修改成功");
    }


    @GetMapping("/{comGroupId}")
    @ApiOperation(value = "查询常用群组信息", notes = "返回JSONObject")
    @ApiImplicitParam(name = "comGroupId", value = "常用群组id")
    public CommonResult<ComuseGroupVO> getOneComUseGroup(@PathVariable String comGroupId) {
        return CommonResult.success(comuseGroupService.getOneComUseGroup(comGroupId), "常用群组信息查询成功");
    }


    @PostMapping("/list-page")
    @ApiOperation(value = "查询分页常用群组信息", notes = "返回JSONObject")
    public CommonResult<IPage<ComuseGroupVO>> getPageComUseGroup(@RequestBody ComuseGroupParam comuseGroupParam) {
        return CommonResult.success(comuseGroupService.getPageComUseGroup(comuseGroupParam), "查询常用群组分页信息成功");
    }


    @PutMapping("/forbid")
    @ApiOperation(value = "禁用常用群组", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.COMUSE_GROUP_MODUL, operType = OperTypeEnum.MOD, operDesc = "禁用常用群组")
    @ApiImplicitParam(name = "comGroupId", value = "常用群组id", dataType = "string")
    public CommonResult stopComuseGroup(String comGroupId) {
        //构造参数
        LambdaUpdateWrapper<ComuseGroup> lambdaUpdateWrapper = Wrappers.<ComuseGroup>lambdaUpdate()
                .eq(ComuseGroup::getComGroupId, comGroupId)
                .set(ComuseGroup::getIsForbid, 0);
        comuseGroupService.update(lambdaUpdateWrapper);
        return CommonResult.success("常用群组禁用成功");
    }
}
